package com.scenic.rd_android.testview.animation;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;

import com.nova.scenic.projectlibs.util.BaseActivity;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;

public class AnimationActivity extends BaseActivity {
    private static final String TAG = "AnimationActivity";
    private ValueAnimatorView valueAnimatorView;

    /*
    after(Animator anim)   将现有动画插入到传入的动画之后执行
    after(long delay)   将现有动画延迟指定毫秒后执行
    before(Animator anim)   将现有动画插入到传入的动画之前执行
    with(Animator anim)   将现有动画和传入的动画同时执行
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        valueAnimatorView = (ValueAnimatorView) findViewById(R.id.custom_valueAnimation);

    }

    public void beginFirstAnimation(final View view) {
        ValueAnimator animation = ValueAnimator.ofObject(new PointEvaluator(), new Point(10, 10), new Point(100, 100));

        animation.setDuration(1000);
        animation.start();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                valueAnimatorView.refresh((Point) animation.getAnimatedValue());
                valueAnimatorView.invalidate();

            }
        });
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
    }


    public void testValueAnimation(View view){
        MyLog.i(TAG, "testValueAnimation " + view);

        ValueAnimator animator = new ValueAnimator(){

        };
//        animator.setIntValues(1,1000);
        animator.setObjectValues(new Point(0, 0), new Point(100, 100));
        animator.setDuration(1000);

        animator.setInterpolator(
                new AccelerateInterpolator() {
            @Override
            public float getInterpolation(float input) {
                MyLog.i(TAG,"input " + input);
                return super.getInterpolation(input);
            }
        });
        animator.setEvaluator(new TypeEvaluator(){
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                MyLog.i(TAG,"type evaluator fraction " + fraction,9 );
//                int startInt = (int) startValue;
//                return (int)(startInt + fraction * ((int)endValue - startInt));
                return new Point(50,50);
            }
        });
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();
                MyLog.i(TAG,"fraction is " + fraction + " value is " + animation.getAnimatedValue());
            }
        });

        animator.start();
    }


    public static class Point {

        private float x;

        private float y;

        public Point(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float getX() {
            return x;
        }

        public float getY() {
            return y;
        }

    }

    public class PointEvaluator implements TypeEvaluator {

        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            Point startPoint = (Point) startValue;
            Point endPoint = (Point) endValue;
            float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
            float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
            Point point = new Point(x, y);
            return point;
        }

    }

}
